package com.zbkj.service.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.URLUtil;
import com.zbkj.common.model.traceable.ZfbDeviceBatches;
import com.zbkj.common.request.*;
import com.github.pagehelper.PageHelper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;

import com.zbkj.common.response.ZfbDeviceBatchesItemsPageDataResponse;
import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.model.traceable.ZfbDeviceBatchesItems;
import com.zbkj.service.dao.device.ZfbDeviceBatchesItemsDao;
import com.zbkj.service.service.ZfbDeviceBatchesItemsService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @author dazongzi
* @description ZfbDeviceBatchesItemsServiceImpl 接口实现
* @date 2025-06-11
*/
@Service
public class ZfbDeviceBatchesItemsServiceImpl extends ServiceImpl<ZfbDeviceBatchesItemsDao, ZfbDeviceBatchesItems> implements ZfbDeviceBatchesItemsService {

    @Resource
    private ZfbDeviceBatchesItemsDao dao;

    @Override
    public Boolean create(ZfbDeviceBatchesItemsSaveRequest zfbDeviceBatchesItemsSaveRequest) {
        ZfbDeviceBatchesItems zfbDeviceBatchesItems = new ZfbDeviceBatchesItems();
        BeanUtils.copyProperties(zfbDeviceBatchesItemsSaveRequest, zfbDeviceBatchesItems, "id");
        boolean save = save(zfbDeviceBatchesItems);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询设备明细表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @param deviceId
     * @return List<ZfbDeviceBatchesItems>
     * @author dazongzi
     * @since 2025-06-11
     */
    @Override
    public List<ZfbDeviceBatchesItemsPageDataResponse> getList(ZfbDeviceBatchesItemsSearchRequest request,
                                                               PageParamRequest pageParamRequest, Integer deviceId) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 ZfbDeviceBatchesItems 类的多条件查询
        LambdaQueryWrapper<ZfbDeviceBatchesItems> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ZfbDeviceBatchesItems::getBatchesId, deviceId);
        lambdaQueryWrapper.eq(ZfbDeviceBatchesItems::getIsDeleted, 0);

        if (ObjectUtil.isNotEmpty(request.getDeviceMac())) {
            lambdaQueryWrapper.like(ZfbDeviceBatchesItems::getDeviceMac, URLUtil.decode(request.getDeviceMac()));
        }

        if (ObjectUtil.isNotEmpty(request.getSn())) {
            lambdaQueryWrapper.eq(ZfbDeviceBatchesItems::getSn, request.getSn());
        }

        if (ObjectUtil.isNotEmpty(request.getIsBad())) {
            lambdaQueryWrapper.eq(ZfbDeviceBatchesItems::getIsBad, request.getIsBad());
        }

        if (ObjectUtil.isNotEmpty(request.getStatus())) {
            lambdaQueryWrapper.eq(ZfbDeviceBatchesItems::getStatus, request.getStatus());
        }

        lambdaQueryWrapper.orderByDesc(ZfbDeviceBatchesItems::getCreatedAt);
        List<ZfbDeviceBatchesItems> activityStyles = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<ZfbDeviceBatchesItemsPageDataResponse> activityStyleResponses = new ArrayList<>();

        activityStyles.stream().forEach(x->{
            ZfbDeviceBatchesItemsPageDataResponse activityStyleResponse = new ZfbDeviceBatchesItemsPageDataResponse();
            BeanUtils.copyProperties(x, activityStyleResponse);
            activityStyleResponses.add(activityStyleResponse);
        });
        return activityStyleResponses;
    }

    @Override
    public void deleteByBatchesId(Integer batchesId) {
        dao.delete(new LambdaQueryWrapper<ZfbDeviceBatchesItems>().eq(ZfbDeviceBatchesItems::getBatchesId, batchesId));
    }

    @Override
    public List<ZfbDeviceBatchesItems> getListByNum(Integer id, int num) {
        LambdaQueryWrapper<ZfbDeviceBatchesItems> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.eq(ZfbDeviceBatchesItems::getBatchesId, id);
        checkWrapper.eq(ZfbDeviceBatchesItems::getStatus, 1);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsBad, 0);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsDeleted, 0);
        checkWrapper.orderByAsc(ZfbDeviceBatchesItems::getId);
        checkWrapper.last("limit " + num);
        return list(checkWrapper);
    }

    @Override
    public int getUnboundCount(Integer id) {
        LambdaQueryWrapper<ZfbDeviceBatchesItems> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.eq(ZfbDeviceBatchesItems::getBatchesId, id);
        checkWrapper.eq(ZfbDeviceBatchesItems::getStatus, 1);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsBad, 0);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsDeleted, 0);
        checkWrapper.orderByAsc(ZfbDeviceBatchesItems::getId);
        return count(checkWrapper);
    }

    @Override
    public List<ZfbDeviceBatchesItems> getWaitBindListByNum(Integer productId, Integer payNum) {
        LambdaQueryWrapper<ZfbDeviceBatchesItems> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.eq(ZfbDeviceBatchesItems::getLinkId, productId);
        checkWrapper.eq(ZfbDeviceBatchesItems::getLinkType, 1);
        checkWrapper.eq(ZfbDeviceBatchesItems::getStatus, 2);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsBad, 0);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsDeleted, 0);
        checkWrapper.eq(ZfbDeviceBatchesItems::getUsedStatus, 0);
        checkWrapper.orderByAsc(ZfbDeviceBatchesItems::getId);
        checkWrapper.last("limit " + payNum);
        return list(checkWrapper);
    }

    @Override
    public List<ZfbDeviceBatchesItems> getLockListByNum(Integer productId, String orderNo) {
        LambdaQueryWrapper<ZfbDeviceBatchesItems> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.eq(ZfbDeviceBatchesItems::getLinkId, productId);
        checkWrapper.eq(ZfbDeviceBatchesItems::getLinkType, 1);
        checkWrapper.eq(ZfbDeviceBatchesItems::getStatus, 2);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsBad, 0);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsDeleted, 0);
        checkWrapper.eq(ZfbDeviceBatchesItems::getUsedStatus, 1);
        checkWrapper.eq(ZfbDeviceBatchesItems::getOrderNo, orderNo);
        checkWrapper.orderByAsc(ZfbDeviceBatchesItems::getId);
        return list(checkWrapper);
    }

    @Override
    public ZfbDeviceBatchesItems getByMac(String mac) {
        LambdaQueryWrapper<ZfbDeviceBatchesItems> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.eq(ZfbDeviceBatchesItems::getDeviceMac, mac);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsBad, 0);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsDeleted, 0);
        checkWrapper.eq(ZfbDeviceBatchesItems::getUsedStatus, 2);
        checkWrapper.last("limit 1");
        return getOne(checkWrapper);
    }

    @Override
    public ZfbDeviceBatchesItems getIdByMac(String mac) {
        LambdaQueryWrapper<ZfbDeviceBatchesItems> checkWrapper = new LambdaQueryWrapper<>();
        checkWrapper.select(ZfbDeviceBatchesItems::getId);
        checkWrapper.eq(ZfbDeviceBatchesItems::getDeviceMac, mac);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsBad, 0);
        checkWrapper.eq(ZfbDeviceBatchesItems::getIsDeleted, 0);
//        checkWrapper.eq(ZfbDeviceBatchesItems::getUsedStatus, 2);
        checkWrapper.last("limit 1");
        return getOne(checkWrapper);
    }

    @Override
    public List<String> getManualMacList() {
        return dao.findManualMacList();
    }

}

